<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ProjectComment;

/**
 * ProjectCommentSearch represents the model behind the search form of `app\models\ProjectComment`.
 */
class ProjectCommentSearch extends ProjectComment
{   
    const PAGESIZE = 20;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'project_id', 'userid', 'star', 'is_top', 'is_read_by_self'], 'integer'],
            [['content', 'picture', 'username', 'created_time', 'updated_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

     /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {   

        $query = ProjectComment::find()->select('p.*,u.nickname as username')->from(ProjectComment::tableName().'as p')->innerJoin(User::tableName().'as u', '`u`.`uid` = `p`.`userid`');

        $dataProvider = new ActiveDataProvider([
            'query' => $query, 
            'pagination' => [
                'pageSize' => self::PAGESIZE,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        
        // grid filtering conditions
        $query->andFilterWhere([
            'p.id' => $this->id,
            'p.project_id' => $this->project_id,
            'p.star' => $this->star,
            'p.is_top' => $this->is_top,
            'p.is_read_by_self' => $this->is_read_by_self,
        ]);

        //创建时间筛选
        if ($this->created_time) {
            //处理时间范围
            $created_time_arr = explode(' - ', $this->created_time);

            $query->andFilterWhere([
                'between', 'created_time',strtotime($created_time_arr[0]), strtotime($created_time_arr[1])
            ]);
        }

        //更新时间筛选
        if ($this->updated_time) {
            //处理时间范围
            $updated_time_arr = explode(' - ', $this->updated_time);
            $query->andFilterWhere([
                'between', 'p.updated_time',strtotime($updated_time_arr[0]), strtotime($updated_time_arr[1])
            ]);
        }

        $query->andFilterWhere(['like', 'p.content', $this->content]);
        $query->andFilterWhere(['like', 'u.nickname', $this->getAttribute('username')]);

        //默认排序
        $dataProvider->sort->defaultOrder = [
            'created_time' => SORT_DESC,
        ];

        //新建排序字段
        $dataProvider->sort->attributes = [
            'id' => [
                'asc' => ['id' => SORT_ASC],
                'desc' => ['id' => SORT_DESC],
            ],
            'created_time' => [
                'asc' => ['created_time' => SORT_ASC],
                'desc' => ['created_time' => SORT_DESC],
            ],
            // 'username' => [
            //     'asc' => ['u.nickname' => SORT_ASC],
            //     'desc' => ['u.nickname' => SORT_DESC],
            // ],
        ];
        return $dataProvider;
    }
}
